// miniprogram/pages/index/index.js
import * as R from 'ramda';
import { Reader } from '../../utils/vendor/ramda-fantasy'
import U from '../../utils/index';
import config from '../../config/index'

const handleReqTaskCount = () => U.handleRequest({}, () => U.reqPost({ url:'/task/get_task_count' }));

// 把后端返回的格式计算出总完成数的函数
const getDoneNum = R.compose(
  R.reduce(R.add, 0),
  R.map(R.prop('ct'))
);

const handleTaskCountRes = res => Reader(
  that => that.setData({
    isShowPage: true,
    pageData: R.clone(res.data),
    numDone: getDoneNum(res.data.total_complete_count_list)
  })
);

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowPage: false,
    pageData: null,
    numDone: null,
    taskTypeNames: config.taskTypeNames,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    R.compose(
      U.fork,
      R.map(
        R.compose(
          R.invoker(1, 'run')(this),
          handleTaskCountRes
        )
      ),
      handleReqTaskCount
    )()
  },
})
